* CPS_merge.do
* 2014.02.05
* Last update 2014.10.28: removed code from older CBSA merge
* Merges geographic variables from CPS onto ATUS 

capture log close
set more off
timer clear 1
timer on 1
clear
set matsize 10000
local work "/DIRECTORY"
cd "`work'"
log using "`work'/logs/CPS_merge.log", replace

* Reading ATUS data
use "`work'/data/atus_comb.dta", clear

* Cleaning
replace hrsersuf = "" if hrsersuf=="-1"
replace hrhhid2 = . if hrhhid2==-1

* Merging additional geographic variables from CPS
compress
merge 1:1 hrhhid pulineno hrhhid2 hryear4 hrmonth hrsersuf using "`work'/Data/CPS_geo.dta", keep(1 3)
drop _merge

* Merging county lat and long
tostring gestfips, replace
gen len = length(gestfips)
replace gestfips = "0" + gestfips if len==1
drop len
gen fips = gestfips + COUNTY
replace fips = "12086" if fips=="12025" /* Assigning Dade County obs the coordinates for Miami-Dade County */
merge m:1 fips using "`work'/data/qcew/County_latlong.dta", keep(1 3) generate(cntygeomerge)
rename CBSA cbsa /* keeping this line so as not to break ATUS_proc.do */

* Saving
saveold "`work'/data/ATUS_with_counties.dta", replace













timer off 1
timer list 1
capture log close





